package com.code
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 *牌工厂类 
	 * @author zqp
	 * 
	 */	
	public class CardFactory
	{
		
		private var allCards:Array;
		//二维数组暂时没用到。
		private var allCardsII:Array;
		private var allCardsBitMap:Bitmap;
		public function CardFactory(bitMap:Bitmap)
		{
			allCardsBitMap = bitMap;
			analysisCars();
		}
		/*解析大图片分成若干纸牌*/
		private function analysisCars():void
		{
			allCardsII = [];
			allCards = [];
			var card:Card;
			var cardX:int = 0,cardY:int = 0;
			var cardTypeArray:Array = [];
			var currCarType:int = 1;
			var currCarNum:int = 1;
			for(var i:int = 1;i<55;i++)
			{
				card = new Card();
				card.cardNum = currCarNum;
				card.cardType = currCarType;
				card.bitmapData.copyPixels(allCardsBitMap.bitmapData,new Rectangle(cardX,cardY,70,95),new Point(0,0));
				cardX += Card.CARD_WIDTH;
				/*暂时没用到*/
				cardTypeArray.push(card);
				/*放入到数组中方便取用*/
				allCards.push(card);
				if(i % 13 == 0)
				{
					cardX = 0;
					cardY += Card.CARD_HEIGHT;
					allCardsII.push(cardTypeArray);
					cardTypeArray = [];
					currCarType += 1;
					currCarNum = 1;
				}else
				{
					currCarNum += 1;
				}
			}
		}
		/**
		 *获得指定的一张牌 
		 * @return 
		 */		
		public function getCard(type:int = 0,num:int = 0):Card
		{
			return 	allCardsII[type - 1][num - 1];
		}
		/**
		 *获得随机牌数组 
		 * @return 
		 */		
		public function getRandomCards():Array
		{
			allCards.sort(taxis);
			return allCards.slice(0,17);
		}
		/**
		 *获取某个区间的随机数 
		 * @param min
		 * @param max
		 * @return 
		 */		
		private function randRange(min:int,max:int):int 
		{
			var randomNum:int = int(Math.random() * (max - min + 1)) + min;
			return randomNum;
		}
		
		/*数组排序用的比较函数*/
		private function taxis(element1:int,element2:int):int
		{
			var num:Number=Math.random();
			if(num<0.5)
			{
				return -1;
			}else
			{
				return 1;
			}
		}
	}
}